<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>参数列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片主体区域 -->
    <el-card>
      <!-- 顶部警告提示框 -->
      <el-row>
        <el-col>
          <el-alert title="注意：只允许第三级分类设置相关参数" type="warning" show-icon :closable="false"></el-alert>
        </el-col>
      </el-row>
      <!-- 级联选择器 -->
      <el-row>
        <el-col>
          请选择商品分类：
          <el-cascader
            v-model="JiLianList"
            expandTrigger="hover"
            :options="canshuList"
            :props="cateProps"
            @change="handleChange"
            clearable
            change-on-select
            class="widthcase"
          ></el-cascader>
        </el-col>
      </el-row>

      <!-- 卡片主体视图区域 -->

      <el-row>
        <el-col>
          <!-- TABS切换栏 -->
          <el-tabs type="border-card" v-model="activeName" @tab-click="gaibiantbs" value="many">
            <!-- 动态参数列表数据 -->
            <el-tab-pane label="动态参数" name="many">
              <!-- 添加参数按钮 -->
              <el-button type="primary" round :disabled="isBtn" @click="Tjiacanshu">添加参数</el-button>
              <el-table border style="width: 100%" :data="ZhutiList">
                <!-- 左侧可展开区域 -->
                <el-table-column type="expand">
                  <template slot-scope="scope">
                    <!-- <div :key="item.id"
                    v-for="item in GetmanyVals">-->
                    <el-tag
                      :key="i"
                      v-for="(val,i) in scope.row.attr_vals"
                     
                      :disable-transitions="false"
                      @close="handleClose(tag)"
                    >{{val}}</el-tag>



                    <!-- 可变化tag添加参数 -->
                    <el-input
                      class="input-new-tag"
                      v-if="inputVisible"
                      v-model="inputValue"
                      ref="saveTagInput"
                      size="small"
                      @keyup.enter.native="handleInputConfirm(scope.row)"
                      @blur="handleInputConfirm"
                    ></el-input>
                    <el-button
                      v-else
                      class="button-new-tag"
                      size="small"
                      @click="showInput"
                    >+ New Tag</el-button>
                    <!-- </div> -->
                  </template>
                </el-table-column>

                <el-table-column type="index" label="#" width="180"></el-table-column>
                <el-table-column prop="attr_name" label="参数名称" width="180"></el-table-column>
                <el-table-column label="操作">
                  <template>
                    <el-button type="primary" icon="el-icon-edit" size="mini">修改</el-button>
                    <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
                  </template>
                </el-table-column>
              </el-table>
            </el-tab-pane>

            <!-- 静态参数列表数据 -->
            <el-tab-pane label="静态参数" name="only">
              <!-- 添加参数按钮 -->
              <el-button type="primary" round :disabled="isBtn">添加属性</el-button>

              <el-table border style="width: 100%" :data="ZhutiList">
                <!-- 左侧可展开区域 -->
                <el-table-column type="expand">
                  <template slot-scope="scope">
                    <el-tag
                      :key="i"
                      v-for="(val,i) in scope.row.attr_vals"
                      
                      :disable-transitions="false"
                      @close="handleClose(tag)"
                    >{{val}}</el-tag>

                    <!-- 可变化tag添加参数 -->
                    <el-input
                      class="input-new-tag"
                      v-if="inputVisible"
                      v-model="inputValue"
                      ref="saveTagInput"
                      size="small"
                      @keyup.enter.native="handleInputConfirm(scope.row)"
                      @blur="handleInputConfirm"
                    ></el-input>
                    <el-button
                      v-else
                      class="button-new-tag"
                      size="small"
                      @click="showInput"
                    >+ New Tag</el-button>
                  </template>
                </el-table-column>

                <el-table-column type="index" label="#" width="180"></el-table-column>
                <el-table-column prop="attr_name" label="参数名称" width="180"></el-table-column>
                <el-table-column label="操作">
                  <template>
                    <el-button type="primary" icon="el-icon-edit" size="mini">修改</el-button>
                    <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
                  </template>
                </el-table-column>
              </el-table>
            </el-tab-pane>
          </el-tabs>
        </el-col>
      </el-row>
    </el-card>

    <!-- 添加静态参数的弹出层 -->
    <el-dialog title="添加静态参数" :visible.sync="TianjiaCs" width="30%">
      <!-- 静态参数输入框 -->
      <el-form :model="JtFrom" :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-form-item label="静态参数" prop="JTName">
          <el-input v-model="JtFrom.attr_name"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="TianjiaCs = false">取 消</el-button>
        <el-button type="primary" @click="TianjiaCsTrue">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 级联选择器数据
      canshuList: [],
      //配置级联菜单中数据如何展示
      cateProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      //   根据id获取的查询参数列表
      categories: [],
      //   绑定级联选择框数据
      JiLianList: [],
      //定义选项卡的name
      activeName: '',
      //   主体区域需要渲染的数据
      ZhutiList: [],
      //添加参数弹出层默认不显示false
      TianjiaCs: false,
      // 静态参数输入框数据
      JtFrom: {
        attr_name: ''
      },
      //   定义添加分类表单的验证规则
      rules: {
        JTName: [
          { required: true, message: '分类名称不能为空', trigger: 'blur' }
        ]
      },
      // 展开行参数
      GetmanyVals: [],
      // 默认input为false
      inputVisible: false,
      // input框数据
      inputValue: ''
    }
  },
  created() {
    this.getfuji()
    console.log(this.ZhutiList)
  },
  methods: {
    //   获取参数分类列表
    async getfuji() {
      const { data: res } = await this.$http.get('categories', {
        params: {
          type: 3
        }
      })
      if (res.meta.status !== 200)
        return this.$message.error('获取父级列表列表失败')
      this.$message.success('获取父级列表成功')
      console.log(res.data)
      this.canshuList = res.data
    },
    // 级联选择器发生改变时调用
    async handleChange() {
      // 如果级联选择器的内容长度不等于3 清空数据
      if (this.JiLianList.length !== 3) {
        this.JiLianList = []
        return
      }
      console.log(this.JiLianList)
      // 调用接口获取3级权限id
      const { data: res } = await this.$http.get(
        `categories/${this.cateId}/attributes`,
        {
          params: { sel: this.activeName }
        }
      )
      if (res.meta.status !== 200) {
        return this.$message.error('获取参数列表数据失败')
      }
      this.$message.success('获取参数列表成功')
      console.log(res.data)
      // 通过循环把rens.data里面的字符串转为数组
      res.data.forEach(i => {
        console.log(i)

        i.attr_vals = i.attr_vals.split(' ')
        this.GetmanyVals += i.attr_vals
      })
      this.GetmanyVals = res.data
      console.log(this.GetmanyVals)

      this.ZhutiList = res.data
      if (this.activeName === 'many') {
        //获取的是动态参数
        this.manyTableData = res.data
      } else if (this.activeName === 'only') {
        //获取的是静态属性
        this.onlyTableData = res.data
      }
    },
    //   tbs改变时触发函数
    gaibiantbs() {
      console.log(this.activeName)
      // 改变时重新调用请求函数 重新渲染数据
      this.handleChange()
    },
    // 点击添加弹出添加框
    Tjiacanshu() {
      // 让添加静态参数的弹出层显示为ture
      this.TianjiaCs = true
    },
    // 点击添加弹出层里的确认按钮调用接口添加数据
    async TianjiaCsTrue() {
      const { data: res } = await this.$http.post(
        `categories/${this.cateId}/attributes`,
        {
          attr_name: this.JtFrom.attr_name,
          attr_sel: 'many',
          attr_vals: 'a,b,c,'
        }
      )
      if (res.meta.status !== 201) {
        return this.$message.error('添加动态参数失败')
      }
      this.$message.success('添加动态参数成功')
      this.TianjiaCs = false
      this.getfuji()

      console.log(res)
    },
    //添加属性input框
    handleClose(tag) {
      this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1)
    },
    showInput() {
      this.inputVisible = true
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },
    handleInputConfirm(val) {
      let inputValue = this.inputValue
      // console.log(inputValue);
      // console.log(val);
      if (inputValue) {
        val.attr_vals.push(this.inputValue)
      }

      this.inputVisible = false
      this.inputValue = ''
    }
  },
  //   计算属性
  computed: {
    //   如果iLianList.length =3 就启用按钮
    isBtn() {
      if (this.JiLianList.length !== 3) {
        return true
      }
      return false
    },
    // 当前选中的三级分类id
    cateId() {
      if (this.JiLianList.length === 3) {
        return this.JiLianList[2]
      }
      return null
    }
  }
}
</script>

<style lang="less" scoped>
.el-cascader {
  margin: 15px 0;
}
</style>